﻿@{
    ViewBag.title = "Статистика CPU";
}
<form method="post">
    <p>
        От
        <input type="text" name="text1" />
        До
        <input type="text" name="text2" />
        <input type="submit" value="Отправить" />
    </p>
    @{        
        var from = ViewBag.AllElem - 10;
        var to = ViewBag.AllElem;
        DateTime fromDateTime = DateTime.Now;
        DateTime toDateTime = DateTime.Now;

        if (IsPost)
        {
            string fromDateTimeStr = Request["text1"].ToString();
            string toDateTimeStr = Request["text2"].ToString();

            if (DateTime.TryParse(fromDateTimeStr, out fromDateTime) && DateTime.TryParse(toDateTimeStr, out toDateTime)
                && DateTime.Compare(fromDateTime, toDateTime) <= 0)
            {
                from = (int)(fromDateTime - @ViewBag.Entries[0].date).TotalSeconds;
                to = (int)(toDateTime - @ViewBag.Entries[0].date).TotalSeconds;
            }
            else
            {
        <p>Данные введены неправильно, введите еще раз, пожалуйста</p>    
            }
        }
        <p>  </p>
    }
</form>

<script type="text/javascript">
    var chart;
    $(document).ready(function () {
        chart = new Highcharts.Chart({
            chart: {
                type: 'spline',
                renderTo: 'graph',
            },
            title: {
                text: 'Загрузка CPU  ',
                x: -5,
                y: 30,
            },
            xAxis: {
                categories: [ @{var i = from; @(i++)} @{
                    for (; i < to; i++)
                    {
                            <text>,</text> @i
                    }
                    }]
            },
            yAxis: {
                title: {
                    text: 'Загрузка CPU'
                },
            },

            series: [{
                name: 'CPU',
                data: [@ViewBag.Entries[from].value @{
                    for (var j = from + 1; j < to; j++)
                    {
                            <text>,</text> @ViewBag.Entries[j].value
                    }
                    }]
            }]
        });
    });
</script>


<div id="graph" style="width: 750px; color: green; height: 400px; margin: 0 auto"></div>

<table>
    <tr>
        <th>Дата</th>
        <th>Процент</th>
    </tr>
    @{ for (var k = from; k < to; k++)
       {
        <tr>
            <td>@ViewBag.Entries[k].date</td>
            <td>@ViewBag.Entries[k].value</td>
        </tr>
       }
    }
</table>